#******************************************************************
#
# Copyright 2016 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

##
# Notification Service c++ wrapper build script
##

import platform
Import('env')

lib_env = env.Clone()
SConscript('#service/third_party_libs.scons', 'lib_env')
notification_env = lib_env.Clone()

target_os = env.get('TARGET_OS')

if notification_env.get('RELEASE'):
    notification_env.AppendUnique(CCFLAGS=['-Os'])
else:
    notification_env.AppendUnique(CCFLAGS=['-g'])

######################################################################
# Build flags
######################################################################
notification_env.AppendUnique(CPPPATH=[
    '../../include',
    'inc',
    '../common',
    '../provider/inc',
    '../../src/common',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/csdk/security/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/include',
    '#/resource/oc_logger/include',
])

notification_env.PrependUnique(LIBS=[
    'oc_logger',
    'oc',
    'notification_consumer',
    'logger',
])

notification_env.AppendUnique(CXXFLAGS=['-std=c++0x', '-frtti'])

if target_os not in ['windows', 'winrt']:
    notification_env.AppendUnique(
        CCFLAGS=['-O2', '-g', '-Wall', '-fmessage-length=0'])

if target_os not in ['darwin', 'ios', 'windows', 'winrt']:
    notification_env.AppendUnique(LINKFLAGS=['-Wl,--no-undefined'])

if target_os == 'linux':
    notification_env.AppendUnique(LIBS=['pthread'])

if target_os == 'android':
    notification_env.AppendUnique(CXXFLAGS=['-frtti', '-fexceptions'])
    notification_env.PrependUnique(LIBS=['gnustl_shared', 'log'])
    notification_env.AppendUnique(
        LINKFLAGS=['-Wl,-soname,libnotification_consumer_wrapper.so'])

if target_os in ['linux', 'tizen']:
    notification_env.AppendUnique(LIBS=['rt'])

######################################################################
# Source files and Targets for Consumer
######################################################################
notification_jni_consumer_env = notification_env.Clone()

Import('notificationCommonStaticObjs')
Import('notificationCommonSharedObjs')

notification_consumer_src = [
    env.Glob('src/*.cpp'), notificationCommonSharedObjs
]

if target_os in ['android']:
    notification_env.AppendUnique(
        LINKFLAGS=['-Wl,-soname,libnotification_consumer_wrapper.so'])
if target_os not in ['ios']:
    consumersdk = notification_env.SharedLibrary(
        'notification_consumer_wrapper', notification_consumer_src)
    notification_env.InstallTarget(consumersdk,
                                   'libnotification_consumer_wrapper')
    notification_env.UserInstallTargetLib(consumersdk,
                                          'libnotification_consumer_wrapper')

notification_consumer_src = [
    env.Glob('src/*.cpp'), notificationCommonStaticObjs
]

consumersdk = notification_env.StaticLibrary(
    'notification_consumer_wrapper', notification_consumer_src)
notification_env.InstallTarget(consumersdk, 'libnotification_consumer_wrapper')
notification_env.UserInstallTargetLib(
    consumersdk, 'libnotification_consumer_wrapper')

notification_env.UserInstallTargetHeader(
    "inc/NSProvider.h", 'service/notification', 'NSProvider.h')
notification_env.UserInstallTargetHeader(
    "inc/NSConsumerService.h", 'service/notification', 'NSConsumerService.h')

######################################################################
# Source files and Targets for Consumer Jni
######################################################################
if target_os == 'android':
    Import('notificationJniCommonSharedObjs')

    notification_jni_consumer_env.AppendUnique(CPPPATH=[
        '../../../../extlibs/boost/boost_1_58_0',
        '../../../../java/jni',
        '../../android/notification-service/src/main/jni/common',
        '../../android/notification-service/src/main/jni/consumer',
    ])

    notification_jni_consumer_env.PrependUnique(
        LIBS=['notification_consumer_wrapper'])

    notification_consumer_jni_src = [
        env.Glob(
            '../../android/notification-service/src/main/jni/consumer/*.cpp'),
        notificationJniCommonSharedObjs
    ]

    consumerJni = notification_jni_consumer_env.SharedLibrary(
        'notification_consumer_jni', notification_consumer_jni_src)
    notification_jni_consumer_env.InstallTarget(consumerJni,
                                                'libnotification_consumer_jni')
    notification_jni_consumer_env.UserInstallTargetLib(
        consumerJni, 'libnotification_consumer_jni')
